(function(win, doc, $){

	function css(element, css){
		for(var i in css){
			element.style[i] = css[i];
		}
		return element;
	}

	function create(tag, cssobj) {
		return css(doc.createElement(tag), cssobj);
	}
	var msie/*@cc_on = doc.documentMode || (doc.compatMode == "CSS1Compat" ? "XMLHttpRequest" in win ? 7 : 6 : 5) @*/;

	function lightbox(html){
		var table, cell, dialog, tabdisp;

		table = create("section", {
			position: "fixed",
			height: "100%",
			width: "100%",
			left: 0,
			top: 0
		});
		cell = create("div", {
			verticalAlign: "middle",
			textAlign: "center"
		});
		dialog = create("dialog", {
			display: "inline-block",
			position: "relative",
			textAlign: "left"
		});
		
		table.className = "lightbox";
		table.appendChild(cell);
		cell.appendChild(dialog);

		if( msie < 8 ){
			css(table, {
				display: "block"
			});
			css(cell, {
				verticalAlign: "baseline",
				wordBreak: "keep-all",
				whiteSpace: "nowrap",
				position: "absolute",
				display: "block",
				width: "100%",
				top: "50%",
				left: 0
			});
			css(dialog, {
				top: "-50%"
			});
			if( msie < 7 ) {
				table.style.position = "absolute";
				function expre(prop, value) {
					setTimeout(function(){
						table.style.setExpression(prop, "offsetParent." + value);
					},1);
				} 
				expre("height", "clientHeight");
				expre("width", "clientWidth");
				expre("top", "scrollTop");
				if(doc.documentElement.currentStyle.backgroundImage == 'none'){
					with(doc.documentElement.style){
						backgroundImage = "url(about:blank)";
						backgroundAttachment = "fixed";
						backgroundRepeat = "no-repeat";
					}
				}
			}
		} else {
			css(table, {
				display: "table"
			});
			css(cell, {
				display: "table-cell"
			});
		}

		css(table, msie < 9 ? {
			filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#33000000,endColorstr=#33000000)",
			background: "url(about:blank)"
		} : {
			background: "rgba(0,0,0,0.2)"
		});

		dialog.innerHTML = html || "";
		(doc.body || doc.documentElement).appendChild(table);
		
		tabdisp = table.style.display;

		if (msie < 8) {
			css(dialog, {
				width: 0
			});
			setTimeout(function(){
				css(dialog, {
					width: dialog.clientWidth + "px"
				});
			},1);
		}

		function toggle(on) {
			table.style.left = on ? 0 : "-9999em"; 
		}
		
		return {
			show: function(){
				toggle(true);
			},
			hide: function(){
				toggle(false);
			},
			toggle: toggle,
			dialog: dialog,
			lightbox: table
		}
	}
	
	if(win.jQuery) {
		jQuery.fn.extend({
			lightbox: function(toggle){
				var box = lightbox(),
				jqobj = $(box.lightbox);
				this.appendTo(box.dialog);
				return $.extend(jqobj, {
					show: function(){
						return this.toggle(true);
					},
					hide: function(){
						return this.toggle(false);
					},
					toggle: function(on){
						box.toggle(on)
						return this;
					}
				});
			},
			dialog: function(){
				var dia = $('<div class="dialog">').html('<h2>' + (this.attr("title") || "&nbsp;") +'</h2><a class="close" href="#">X</a>').append(this),
					box = dia.lightbox();
				
				dia.children(".close").click(function(){
					box.hide();
				});
				return box;
			}
		});

		jQuery.extend({
			msgbox: function(msg, fun, title){
				var dia = $('<div class="dialog msgbox"><h2>' + (title || $.msgbox.title || "Message") + '</h2><div class="cont">' + (msg || "&nbsp;") + '</div><div class="btns"><input type="submit" value="确定">' + (fun ? '<input type="reset" value="取消">' : "") + '</div></div>'),
				box = dia.lightbox();
				dia.find(".btns input").click(function(){
					box.hide();
					if(fun) {
						fun.call(win, this.type == "submit");
					}
				});
				return box;
			},
			infobox: function(info){
				var box = $('<div class="infobox"><div>' + info + '<a href="#" class="close" onClick="var box = this.parentNode.parentNode; box.parentNode.removeChild(box);">X</a></div></div>').appendTo(doc.documentElement);
				setTimeout(function(){
					box.slideUp(function(){
						box.remove();
					});
				}, 5000);
				return box;
			}
		});
	}
	
	$(function(){
		$.extend({
			loading: $('<div id="loading">').lightbox().hide()
		});
		var t = 0;
		setInterval(function(){
			if( t > 3 ){
				t = 0;
			}
			$("#loading").css({
				transform: "rotate(" + t*90 + "deg)"
			});
			t++;
		}, 400);
	});

	$.msgbox.title = "消息";

})(this, this.document, this.jQuery);